package com.aviccxzx.smarthome.UserModule;

import android.Manifest;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.aviccxzx.smarthome.R;
import com.aviccxzx.smarthome.base.GosUserModuleBaseActivity;
import com.aviccxzx.smarthome.utils.DialogUtil;

import java.util.List;

import pub.devrel.easypermissions.AfterPermissionGranted;
import pub.devrel.easypermissions.AppSettingsDialog;
import pub.devrel.easypermissions.EasyPermissions;


/**
 * Created by joey on 2016/10/25.
 */
public class VideoResetActivity extends GosUserModuleBaseActivity implements View.OnClickListener, EasyPermissions.PermissionCallbacks {

    EditText content;//定义一个文本输入框

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            setContentView(R.layout.video_reset_lay_v19);
        } else {
        }
        ((TextView) findViewById(R.id.title_tv)).setText("等待配置");
        String yst = getIntent().getStringExtra("yst");
        spf.edit().putString("yst", yst).commit();
        findViewById(R.id.ll_title_left_view).setOnClickListener(this);
        findViewById(R.id.get_heared).setOnClickListener(this);
        showInfo();
    }

    public void showInfo() {
        new DialogUtil(this, true, true, "添加摄像机仅支持2.4G网络，请确认连接的是2.4G网络（可通过查询家用路由器的网络无线速率进行确认）", "取消", "确定", new DialogUtil.SureInterfance() {
            @Override
            public void sureTodo() {
            }

            @Override
            public void cancle() {

            }
        }).showCustomDialog();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_title_left_view:
                finish();
                break;
            case R.id.get_heared:
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    readPhone();
                } else {
                    startEnterWifi();
                }
                break;
        }
    }

    public void startEnterWifi() {
        Intent setwifi = new Intent(VideoResetActivity.this, EnterWifiPwdActivity.class);
        setwifi.putExtra("video", "video");
        startActivity(setwifi);
    }

    /**
     * 请求操作文件夹权限码
     */
    public static final int REQUEST_READPHONE_PERM = 114;

    /**
     * EsayPermissions接管权限处理逻辑
     *
     * @param requestCode
     * @param permissions
     * @param grantResults
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        // Forward results to EasyPermissions
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }

    @AfterPermissionGranted(REQUEST_READPHONE_PERM)
    public void readPhone() {
        if (EasyPermissions.hasPermissions(VideoResetActivity.this, Manifest.permission.READ_PHONE_STATE)) {
            // Have permission, do the thing!
            startEnterWifi();
        } else {
            // Ask for one permission
            EasyPermissions.requestPermissions(this, "需要获取手机状态信息的权限",
                    REQUEST_READPHONE_PERM, Manifest.permission.READ_PHONE_STATE);
        }
    }

    @Override
    public void onPermissionsGranted(int requestCode, List<String> perms) {
        switch (requestCode) {
            case REQUEST_READPHONE_PERM:
                startEnterWifi();
                break;
        }
    }

    @Override
    public void onPermissionsDenied(int requestCode, List<String> perms) {
        if (EasyPermissions.somePermissionPermanentlyDenied(this, perms)) {
            new AppSettingsDialog.Builder(this, VideoResetActivity.this.getResources().getString(R.string.readphone_show))
                    .setTitle("权限申请")
                    .setPositiveButton("确认")
                    .setNegativeButton("取消", null /* click listener */)
                    .setRequestCode(REQUEST_READPHONE_PERM)
                    .build()
                    .show();
        }
    }
}
